package com.qin.mall.controller.productcontroller;

import com.qin.mall.common.api.CommonPage;
import com.qin.mall.common.api.CommonResult;
import com.qin.mall.domain.PmsProductAttribute;
import com.qin.mall.domain.PmsProductCategoryAttributeRelation;
import com.qin.mall.dto.ProductAttributeParam;
import com.qin.mall.openfeign.product.ProductAttributeFeigin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性管理
 * @author Leisure
 * create 2022 2022/11/19 16:45
 **/
@RestController
@RequestMapping("/productAttribute")
public class AttributeController {
    
    @Autowired
    private ProductAttributeFeigin attributeFeigin;

    //根据分类分页获取商品属性
    @GetMapping("/list/{cid}")
    public CommonResult<CommonPage<PmsProductAttribute>> getListByType(@PathVariable Long cid,
                                                                       @RequestParam(value = "type") Integer type,
                                                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        CommonResult<CommonPage<PmsProductAttribute>> listByType = attributeFeigin.getListByType(cid, type, pageSize, pageNum);
        return listByType;
    }

    //添加商品属性
    @PostMapping("/create")
    public CommonResult createAttribute(@RequestBody ProductAttributeParam productAttributeParam){
        CommonResult attribute = attributeFeigin.createAttribute(productAttributeParam);
        return attribute;
    }

    //修改商品属性
    @PostMapping("/update/{id}")
    public CommonResult updateAttribute(@PathVariable Long id, @RequestBody ProductAttributeParam productAttributeParam){
        CommonResult count = attributeFeigin.updateAttribute(id, productAttributeParam);
        return count;
        
    }

    // 获取单个商品属性信息
    @GetMapping("/{id}")
    public CommonResult<PmsProductAttribute> getAttributeById(@PathVariable Long id){
        CommonResult<PmsProductAttribute> attribute = attributeFeigin.getAttributeById(id);
        return attribute;
    }

    //删除多个商品属性值
    @PostMapping("/delete")
    public CommonResult deleteAttributeByIds(@RequestBody List<Long> ids){
        CommonResult count = attributeFeigin.deleteAttributeByIds(ids);
        return count;
    }

    // 这里可能存在问题--根据分类获取商品属性信息
    @GetMapping("/attrInfo/{productCategoryId}")
    public CommonResult<List<PmsProductCategoryAttributeRelation>> getProductAttrInfo(@PathVariable Long productCategoryId){
        CommonResult<List<PmsProductCategoryAttributeRelation>> productAttrInfo = attributeFeigin.getProductAttrInfo(productCategoryId);
        return productAttrInfo;
    }

}
